package br.com.iswe.gwt.ui.client;

import java.sql.Timestamp;
import java.util.Date;

import com.google.gwt.user.client.ui.Label;

public class DateTimeLabel extends Label implements FormProperty{

	private String name;
	private Boolean enabled = true;
	
	private Date dateValue;
	private Timestamp timestampValue;
	
	public void clear() {
		setText("");
	}

	public String getName() {
		return name;
	}

	public String getStringValue() {
		return getText();
	}
	
	public Object getValue() {
		if(dateValue != null){
			return dateValue;
		}
		else if(timestampValue != null){
			return timestampValue;
		}
		else{
			if(ClientUtils.isNotEmpty(getText())){
				DateInfo dtInfo = new DateInfo(getText());
				return dtInfo.getTimestamp();
			}
			else{
				return null;
			}
		}
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setStringValue(String value) {
		setValue(value);
	}
	
	public void setValue(Object value) {
		if(value == null){
			setText("");
		}
		
		if(value instanceof Date){
			DateInfo dt = new DateInfo((Date)value);
			setText(dt.format());
			this.dateValue = (Date)value;
		}
		else if(value instanceof Timestamp){
			DateInfo dt = new DateInfo((Timestamp)value);
			setText(dt.format());
			this.timestampValue = (Timestamp)value;
		}
		else{
			setText(value.toString());
		}
	}
	
}